package com.chenfubing.ps.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfubing.ps.entity.User;
import com.chenfubing.ps.entity.result.JsonResult;
import com.chenfubing.ps.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenfubing
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 添加用户
     *
     * @param user
     */
    @PostMapping("/addUser")
    public JsonResult addUser(@Validated @RequestBody User user) {
        iUserService.addUser(user);
        return new JsonResult();
    }

    /**
     * 获取用户分页
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getUserPage")
    public JsonResult getUserPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return new JsonResult(iUserService.getUserPage(pageNum, pageSize));
    }

    /**
     * 根据id查询用户
     *
     * @param userId
     * @return
     */
    @GetMapping("/findUser")
    public JsonResult findUser(@RequestParam int userId) {
        return new JsonResult(iUserService.findUser(userId));
    }

    /**
     *
     * @param carLicense
     * @return
     */
    @GetMapping("/findUserByLicense")
    public JsonResult findUserByLicense(@RequestParam String carLicense){
        return new JsonResult(iUserService.findUserByLicense(carLicense));
    }

    @GetMapping("/deleteUser")
    public JsonResult deleteUser(@RequestParam int id)
    {
        return new JsonResult(iUserService.getBaseMapper().deleteById(id));
    }

}
